#include "shop.h"
#include "ui_shop.h"

Shop::Shop(QWidget *parent, int id) :
    QWidget(parent),
    ui(new Ui::Shop)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::FramelessWindowHint);      // 隐藏边框
    this->setAttribute(Qt::WA_TranslucentBackground);     // 背景透明
    this->id = id;         // 传递用户id
    this->parent = parent;

    // 连接数据库
    dbase = new DbOperator();
    dbase->OpenDb();
    msg = dbase->searchTrainerMsg(id);

}

Shop::~Shop()
{
    delete ui;
    if (dbase != 0) {
        delete dbase;
        dbase = 0;
    }
}


void Shop::closeEvent(QCloseEvent *) {
    emit ExitShop();   // 关闭时发射信号
}


void Shop::on_s1_clicked()
{
    // 默认的警示框样式
    //    int ret = QMessageBox::question(this, tr("确认"),
    //                                    tr("确认购买此商品吗？"),
    //                                    QMessageBox::Yes,
    //                                    QMessageBox::No);
    //    if(ret == QMessageBox::Yes) {
    //        dbase->updateGoldCoin(id, msg.goldcoin-20);
    //        QMessageBox::information(this, tr("购买成功"),
    //                                 tr("商品购买成功"),
    //                                 QMessageBox::Ok);

    //        qDebug() << "商品购买成功";
    //    }
    a = new alert(parent);
    connect(a, SIGNAL(yes()), this, SLOT(updateCoin1()));
    a->move(720, 450);
    a->show();
}

void Shop::updateCoin1()
{
    dbase->updateGoldCoin(id, msg.goldcoin-20);
    qDebug() << "商品购买成功";
    getnewItem(1);
}

void Shop::updateCoin2()
{
    dbase->updateGoldCoin(id, msg.goldcoin-30);
    qDebug() << "商品购买成功";
    getnewItem(2);

}

void Shop::updateCoin3()
{
    dbase->updateGoldCoin(id, msg.goldcoin-40);
    qDebug() << "商品购买成功";
    getnewItem(3);

}

void Shop::updateCoin4()
{
    dbase->updateGoldCoin(id, msg.goldcoin-50);
    qDebug() << "商品购买成功";
    getnewItem(4);

}

void Shop::updateCoin5()
{
    dbase->updateGoldCoin(id, msg.goldcoin-100);
    qDebug() << "商品购买成功";
    getnewItem(5);

}

void Shop::updateCoin6()
{
    dbase->updateGoldCoin(id, msg.goldcoin-200);
    qDebug() << "商品购买成功";
    getnewItem(6);

}

void Shop::getnewItem(int no)
{
    QString temp;
    QString old = msg.item;
    // 先分割
    QStringList l = old.split(".");
    // 对应位置转化为数字并自增一
    temp = l.at(no-1);
    int num = temp.toInt();
    num++;
    // 重新拼接
    temp = QString::number(num);
    l.replace(no-1, temp);
    temp = l.join(".");
    qDebug() << temp;
     // 更新数据库中数据
    dbase->updateItem(id, temp);
    // 更新界面显示的数据库数据
    msg = dbase->searchTrainerMsg(id);
}

void Shop::on_closebtn_clicked()
{
    this->close();
    delete dbase;
    dbase = 0;
}

void Shop::on_s2_clicked()
{
    a = new alert(parent);
    connect(a, SIGNAL(yes()), this, SLOT(updateCoin2()));
    a->move(750, 400);
    a->show();
}

void Shop::on_s3_clicked()
{
    a = new alert(parent);
    connect(a, SIGNAL(yes()), this, SLOT(updateCoin3()));
    a->move(750, 400);
    a->show();

}

void Shop::on_s4_clicked()
{
    a = new alert(parent);
    connect(a, SIGNAL(yes()), this, SLOT(updateCoin4()));
    a->move(750, 400);
    a->show();

}

void Shop::on_s5_clicked()
{
    a = new alert(parent);
    connect(a, SIGNAL(yes()), this, SLOT(updateCoin5()));
    a->move(750, 400);
    a->show();

}

void Shop::on_s6_clicked()
{
    a = new alert(parent);
    connect(a, SIGNAL(yes()), this, SLOT(updateCoin6()));
    a->move(750, 400);
    a->show();
}
